﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed;
    public float damage;
    public GameObject ExplosionEffect;
    private GameObject enemy;

    public virtual void FollowEnemy(GameObject enemy)
    {
        this.enemy = enemy;
    }

    protected void Update()
    {
        BulletAttack();
    }

    protected virtual void BulletAttack()
    {
        if (enemy != null)
        {
            transform.LookAt(enemy.transform.position);
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
            if (Vector3.Distance(transform.position, enemy.transform.position) < 0.3f)
            {
                enemy.GetComponent<Enemy>().GetHurt(damage);
                BulletDestroy();
            }
        }
        else
        {
            BulletDestroy();
        }
    }

    protected void BulletDestroy()
    {
        Destroy(this.gameObject);
        GameObject go = GameObject.Instantiate(ExplosionEffect, transform.position,Quaternion.identity);
        Destroy(go,0.2f); //爆炸特效
    }
}
